package com.diet.web.controller.diet;

import com.diet.common.annotation.Log;
import com.diet.common.core.controller.BaseController;
import com.diet.common.core.domain.AjaxResult;
import com.diet.common.core.page.TableDataInfo;
import com.diet.common.enums.BusinessType;
import com.diet.common.utils.poi.ExcelUtil;
import com.diet.system.domain.DietUser;
import com.diet.system.domain.DietUserBehavior;
import com.diet.system.service.IDietUserBehaviorService;
import com.diet.system.service.IDietUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 饮食用户Controller
 */
@RestController
@RequestMapping("/diet/user")
public class DietUserController extends BaseController
{
    @Autowired
    private IDietUserService dietUserService;
    @Autowired
    private IDietUserBehaviorService dietUserBehaviorService;

    /**
     * 查询饮食用户列表
     */
    @PreAuthorize("@ss.hasPermi('diet:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(DietUser dietUser)
    {
        startPage();
        List<DietUser> list = dietUserService.selectDietUserList(dietUser);
        return getDataTable(list);
    }

    /**
     * 导出饮食用户列表
     */
    @PreAuthorize("@ss.hasPermi('diet:user:export')")
    @Log(title = "饮食用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DietUser dietUser)
    {
        List<DietUser> list = dietUserService.selectDietUserList(dietUser);
        ExcelUtil<DietUser> util = new ExcelUtil<DietUser>(DietUser.class);
        util.exportExcel(response, list, "饮食用户数据");
    }

    /**
     * 获取饮食用户详细信息
     */
    @PreAuthorize("@ss.hasPermi('diet:user:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") String userId)
    {
        return AjaxResult.success(dietUserService.selectDietUserByUserId(userId));
    }

    /**
     * 新增饮食用户
     */
    @PreAuthorize("@ss.hasPermi('diet:user:add')")
    @Log(title = "饮食用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DietUser dietUser)
    {
        return toAjax(dietUserService.insertDietUser(dietUser));
    }

    /**
     * 修改饮食用户
     */
    @PreAuthorize("@ss.hasPermi('diet:user:edit')")
    @Log(title = "饮食用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DietUser dietUser)
    {
        return toAjax(dietUserService.updateDietUser(dietUser));
    }

    /**
     * 删除饮食用户
     */
    @PreAuthorize("@ss.hasPermi('diet:user:remove')")
    @Log(title = "饮食用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable String[] userIds)
    {
        return toAjax(dietUserService.deleteDietUserByUserIds(userIds));
    }

   /**
 * 获取用户行为数据
 */
@PreAuthorize("@ss.hasPermi('diet:user:behavior')")
@GetMapping("/behavior/{userId}")
public AjaxResult getBehavior(@PathVariable("userId") String userId)
{
    try {
        // 查询用户的收藏记录
        List<DietUserBehavior> favorites = dietUserBehaviorService.selectUserFavorites(Long.valueOf(userId));

        // 查询用户的评分记录
        List<DietUserBehavior> ratings = dietUserBehaviorService.selectUserRatings(Long.valueOf(userId));

        // 查询用户的点击记录
        List<DietUserBehavior> clicks = dietUserBehaviorService.selectUserClicks(Long.valueOf(userId));

        // 构建返回结果
        Map<String, Object> result = new HashMap<>();
        result.put("favoritesCount", favorites.size());
        result.put("ratingsCount", ratings.size());
        result.put("clicksCount", clicks.size());
        result.put("favorites", favorites);
        result.put("ratings", ratings);
        result.put("clicks", clicks);

        return AjaxResult.success(result);
    } catch (Exception e) {
        return AjaxResult.error("获取用户行为数据失败：" + e.getMessage());
    }
}

    /**
 * 查询用户浏览记录
 */
    @PreAuthorize("@ss.hasPermi('diet:user:behavior')")
 @GetMapping("/clicks/{userId}")
public TableDataInfo getUserClicks(@PathVariable("userId") Long userId) {
    startPage();
    List<DietUserBehavior> clicks = dietUserBehaviorService.selectUserClicks(userId);
    System.out.println("getUserClicks：" + clicks);
    return getDataTable(clicks);
}

}